package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.SysDictMapper;
import com.shangyueshang.model.SysDict;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.request.SysDictDTO;
import com.shangyueshang.response.HttpResult;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.SysDictService;
import com.shangyueshang.utils.MyPageHelper;
import com.shangyueshang.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/5 17:44
 * @version: v1.0.0
 */
@Service
public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> implements SysDictService {
	
	@Override
	public List<SysDict> findByLabel(String label) {
		LambdaQueryWrapper<SysDict> queryWrapper =
				new LambdaQueryWrapper<SysDict>()
						.eq(
								Objects.nonNull(label),
								SysDict::getLabel,
								label
						);
		return super.list(queryWrapper);
	}
	
	@Override
	public boolean save(SysDict entity) {
		entity.setCreator(SecurityUtil.getUsername());
		entity.setLastUpdator(SecurityUtil.getUsername());
		return super.save(entity);
	}
	
	@Override
	public PageResult findPage(PageReq pageReq) {
		Map<String, Object> params = pageReq.getLineParams();
		QueryWrapper<SysDict> wrapper =
				new QueryWrapper<SysDict>().orderByDesc(SysConstants.create_time);
		params.forEach(wrapper::eq);
		PageInfo<SysDict> pageInfo =
				PageHelper.startPage(
						pageReq.getPageNum(),
						pageReq.getPageSize()
				)
						.doSelectPageInfo(
								() -> baseMapper.selectList(wrapper)
						);
		return MyPageHelper.getPageResult(pageInfo);
	}
	
	@Override
	public List<SysDict> findByType(String type) {
		return super.list(
				new LambdaQueryWrapper<SysDict>()
						.eq(
								Objects.nonNull(type),
								SysDict::getType, type)
		);
	}
	
	@Override
	public HttpResult saveUpdate(SysDictDTO dict) {
		List<SysDict> dictList = super.list(
				new LambdaQueryWrapper<SysDict>()
						.eq(
								SysDict::getLabel,
								dict.getLabel()
						)
						.eq(
								SysDict::getType,
								dict.getType()
						)
		);
		if (dict.getId() != null) {
			if (!dictList.stream().filter(i -> !i.getId().equals(dict.getId())).collect(Collectors.toList()).isEmpty()) {
				return HttpResult.fail("该类型已存在同样的名称了");
			}
		}
		SysDict sysDict = new SysDict();
		BeanUtils.copyProperties(dict, sysDict);
		return HttpResult.ok(super.saveOrUpdate(sysDict));
	}
	
	@Override
	public Map<String, List<SysDict>> findByTypes(List<String> types) {
		List<SysDict> dictList = super.list(
				new LambdaQueryWrapper<SysDict>()
						.in(!types.isEmpty(), SysDict::getType, types)
		);
		Map<String, List<SysDict>> map = dictList.stream().collect(Collectors.groupingBy(SysDict::getType));
		types.forEach(
				type -> {
					if (Objects.isNull(map.get(type))) {
						map.put(type, Collections.emptyList());
					}
				}
		);
		return map;
	}
}
